package com.Beans.Dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.Beans.Models.Servicioinformatico;
import com.Beans.Dao.HibernateUtil;

public class ServicioinformaticoDAO {
	
	public static Servicioinformatico findById(int id){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		Servicioinformatico si = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM servicioinformatico si WHERE si.id= :id");
			q.setParameter("id", id);
			si = (Servicioinformatico)q.uniqueResult();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return si;
	}
	
	public static List<Servicioinformatico> findAll(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		List <Servicioinformatico> si = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM servicioinformatico");
			si = (List<Servicioinformatico>)q.list();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return si;
	}
	
	public static List<Servicioinformatico> findByRecursoInformatico(int id_ri){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		List <Servicioinformatico> si = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM servicioinformatico si WHERE si.idri = :id_ri");
			q.setParameter("id_ri", id_ri);
			si = (List<Servicioinformatico>)q.list();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return si;
	}
}
